Filtering examples
This simulation file provides examples of using the different filtering components included in CLEM.
Ruminant filters simulation
This simulation shows the range of ruminant filters provided.
The simulation creates a Ruminant herd with a variety of individuals. We will use the Ruminant herd report component provided as a child of the Activities holder to see the results of filtering. This Herd report component reports a list of individuals each time step (a single month in this example based on the Clock settings). The APSIM Report component in the Reports folder is set to report whenever the [ReportHerd].OnReportItemGenerated event is fired. This event is fired by ReportHerd component once for each individual selected each time step, and provides the Ruminant object for the report to access and write the property values to the output database. The individual is accessed in the report through [ReportHerd].ReportDetails.Individual in the variables section of the Report properties. This example also includes a Resource ledger report reporting each individual created (initialised) in the simulation to compare the entire population.
The Ruminant herd report expects a Ruminant group component as a child which is used to provide the rules to filter the herd and supply the individuals for reporting. A selection of various Filter types are provided as children of the Ruminant filter group and all are disabled when you open the simulation. You can enable and disable the components supplied, run the model, and view the output data (Select the Reports.Report, and see the Data Tab). This will show the individuals selected and reported to the database.
This next section will guide you through the expected functionality.

While a Ruminant group is required below the Ruminant herd report, the default is that it has no filters as children which will return the entire herd. You will see the herd is reported in the same order as the individuals were created and reported in the HerdLedger as this is how they appear in the underlying lists.

The Sort randomise component will shuffle the individuals returned from filtering. Including this component you will see the individuals reported are no longer in the same order as the herd ledger. You can include this component with any other filter provided to randomise the individuals returned and remove the inherent underlying order from creation of individuals.

A range examples use the Filter by property component. This allows a rule to compare against one of the filterable Ruminant properties. The first example FilterByPropertyIsPregnant uses Female.IsPregnant equals True . This will return all pregnant females. The properties that start with the sex and a period (Female. and Male.) will automatically filter on Sex = Female and Sex=Male respectively. The next FilterByPropertyIsHeifer uses the simplified IsTrue check. the same approach is used in FilterBySires, FilterByMales, FilterByLactating. FilterByAgeLessThan12 is the first case that does not use a sex specific term so will relate to males and females. The rule uses the AgeInYears converter to check that the age in years (decimal) is less than 1. FilterByAbleToBreed is an example of using a more complex property that checks a range of criteria relating to the individuals, age, weight, condition and timing since other pregnancy actions if female, to determine the list to return.
See what combinations of these filters return with some of the checks performed provided in the Results Memo.

The Filter by attribute component utilises the individual attribute functionality of the model. These attributes are akin to a tag assigned to the individual and can also have an associated value. We test whether a particular tag has been assigned to individuals in FilterByGenoIndexExists and do a comparison against the associated value further limiting the individuals selected in FilterByGenoIndexGT50 (value greater than 50). FilterByGenoA uses the short from IsTrue and Exists as assignment style.

As previously discussed you may need to remove underlying arrangement of individuals with randomisation. There are also times when you need to apply an activity task to individuals in a particular order. To achieve this we use the Sort by property component. The SortByProperty example will return all individuals by weight descending so the largest individual first. We can also sort a boolean (true or false) property as seen in FilterByHeifer where true and false are represented by one and zero respectively, so all non-heifers will be reported first and heifers next. These groups will maintain their underlying order so can be combined with a random or property filter such as SortByProperty to return each group in descending weight order.

We can also use Sort by attribute components to use the attributes and values in the sorting rules. The SortByAttributeGenoIndexExists and SortByAttributeGenoIndexValue examples are provided. You will notice these two components allow you to specify the value to use if the attribute is not assigned to an individual when sorting. This will allow you to put these individuals at the start or end of the list returned.

Finally there are components that allow you to decide what proportion of the selected individuals you want to return. The Take from filtered component allows you to take either a proportion (TakeFromFilteredProportion) or a specified number of individuals (TakeFromFilteredIndividuals, top 3) from either the beginning or the end of the list. It is worth including a Sort by property or Sort randomise component as well if you want a the individuals sorted or fully random before selecting.
See all examples